package cn.iocoder.yudao.module.system.controller.admin.alert;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.alert.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.alert.AlertDO;
import cn.iocoder.yudao.module.system.service.alert.AlertService;

@Tag(name = "管理后台 - 预警信息")
@RestController
@RequestMapping("/system/alert")
@Validated
public class AlertController {

    @Resource
    private AlertService alertService;

    @PostMapping("/create")
    @Operation(summary = "创建预警信息")
    @PreAuthorize("@ss.hasPermission('system:alert:create')")
    public CommonResult<Long> createAlert(@Valid @RequestBody AlertSaveReqVO createReqVO) {
        return success(alertService.createAlert(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新预警信息")
    @PreAuthorize("@ss.hasPermission('system:alert:update')")
    public CommonResult<Boolean> updateAlert(@Valid @RequestBody AlertSaveReqVO updateReqVO) {
        alertService.updateAlert(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预警信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:alert:delete')")
    public CommonResult<Boolean> deleteAlert(@RequestParam("id") Long id) {
        alertService.deleteAlert(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预警信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:alert:query')")
    public CommonResult<AlertRespVO> getAlert(@RequestParam("id") Long id) {
        AlertDO alert = alertService.getAlert(id);
        return success(BeanUtils.toBean(alert, AlertRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得预警信息分页")
    @PreAuthorize("@ss.hasPermission('system:alert:query')")
    public CommonResult<PageResult<AlertRespVO>> getAlertPage(@Valid AlertPageReqVO pageReqVO) {
        PageResult<AlertDO> pageResult = alertService.getAlertPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AlertRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出预警信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:alert:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAlertExcel(@Valid AlertPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AlertDO> list = alertService.getAlertPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "预警信息.xls", "数据", AlertRespVO.class,
                        BeanUtils.toBean(list, AlertRespVO.class));
    }

}